package com.example.myui.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.example.myui.R;
import com.example.myui.utils.DensityUtil;

/**
 * Created by Administrator on 2018/1/18/018.
 */

public class MyCircle extends ProgressBar {

    private static final int DEFAULT_WIDTH = 10;
    private static final int REACH_COLOR = Color.parseColor("#ffff00");
    private static final int UNREACH_COLOR = Color.parseColor("#ff0000");

    private int mWidth = DensityUtil.dp2px(DEFAULT_WIDTH);
    private int mReachColor = REACH_COLOR;
    private int mUnReachColor = UNREACH_COLOR;
    private Paint mUnReachPaint;
    private Paint mReachPaint = new Paint();
    private int min;

    public void setSweepAngle(int sweepAngle) {
        this.sweepAngle = sweepAngle;
        invalidate();
    }

    public int getSweepAngle() {
        return sweepAngle;
    }

    private int sweepAngle = 0;

    public MyCircle(Context context) {
        this(context, null);
    }

    public MyCircle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("ResourceAsColor")
    public MyCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Circle, defStyleAttr, 0);
        mWidth = ta.getInt(R.styleable.Circle_circle_width, mWidth);
        mReachColor = ta.getInt(R.styleable.Circle_circle_reach_color, mReachColor);
        mUnReachColor = ta.getInt(R.styleable.Circle_circle_unreach_color, mUnReachColor);
        mUnReachPaint = new Paint();
        mUnReachPaint.setStrokeWidth(mWidth);
        mUnReachPaint.setStyle(Paint.Style.STROKE);
        mUnReachPaint.setColor(mUnReachColor);

        mReachPaint.setStyle(Paint.Style.STROKE);
        mReachPaint.setStrokeWidth(mWidth);
        mReachPaint.setColor(mReachColor);
        ta.recycle();

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        min = Math.min(widthSize, heightSize);
        setMeasuredDimension(min, min);

    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
//        canvas.drawArc(new RectF(0, 0, min, min), 0, 90, false, mUnReachPaint);
        int strokeWidth = (int) mUnReachPaint.getStrokeWidth();
//        canvas.translate(min / 2, min / 2);
        Paint paint = new Paint();
        paint.setColor(mReachColor);
        paint.setStrokeWidth(mWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);


        RectF rectF = new RectF(0 + mWidth / 2, 0 + mWidth / 2, min - mWidth / 2, min - mWidth / 2);
//        canvas.drawOval(rectF,paint);
        canvas.drawRect(rectF, paint);
        canvas.drawArc(rectF, 0, 360, false, mUnReachPaint);
        canvas.drawArc(rectF, 0, sweepAngle, false, mReachPaint);

//        canvas.drawArc(rectF, 0, 90, false, mReachPaint);
//        canvas.drawCircle(0, 0, min / 2 - strokeWidth / 2, mUnReachPaint);
//        canvas.drawCircle(0, 0, min / 2 - strokeWidth / 2, mUnReachPaint);
//        canvas.drawCircle(0, 0, min / 2 - strokeWidth / 2, mReachPaint);
        canvas.restore();
    }
}
